<?php
$manageBookableResourceStr = Yii::t('site','Manage Bookable Resource');
$setupResourceStr = Yii::t('site','Setup resource');
$choosePrimaryResourceStr = Yii::t('site','Choose Primary Resource');
$setAvailableAmountStr = Yii::t('site','Set available amount');
$chooseTimeSlotTypeStr = Yii::t('site','Choose Time Slot Type');
$setUnitPricetStr = Yii::t('site','Set unit price');
$setTimeSlotStr = Yii::t('site','Setup time slot');
$setStartHourStr = Yii::t('site','Set start hour');
$setEndHourStr = Yii::t('site','Set end hour');
$weeklyRepeatDaysStr = Yii::t('site', 'Weekly repeat days');
$clockStr = Yii::t('site','Clock');
$weekDayStr = Yii::t('site','week-day');

$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
$app = Blyn::app()->getActiveApp();
$rList = $app->getResourceList(array('format' => 'stringList'));

$defaultTimeslotId = 0;

$bResourceModel = new BSBookableResourceFormModel();
$bResourceTimeslotModel = new BSTimeslotFormModel();
$isPost = false;
if (!empty($_POST)) {
     if(isset( $_POST['bResourceId' . $bResourceId]) )
     {
        $bResourceModel->id = $bResourceId;
        $bResourceModel->timeslotId = $_POST['timeslotId' . $bResourceId];
        $bResourceModel->resourceId = $_POST['bResourceId' . $bResourceId];
        $bResourceModel->amount = $_POST['amount' . $bResourceId];
        $bResourceModel->unitPrice = $_POST['unitPrice' . $bResourceId];
    //    $bResourceModel->bookingOpenTime = $_POST['bookingOpenTime' . $bResourceId];
    //    $bResourceModel->bookingEndTime = $_POST['bookingEndTime' . $bResourceId];
        
        if ($bResourceModel->timeslotId == "") {
            $bResourceModel->repeatStartTime = $_POST['repeatStartTime' . $bResourceId];
            $bResourceModel->repeatEndTime = $_POST['repeatEndTime' . $bResourceId];
            $bResourceTimeslotModel->description = $_POST['weeklyRepeatDay' . $bResourceId];
        }
        
        $bResourceTimeslotModel->id = $bResourceModel->timeslotId;
     }
     else if($bResourceId>0) {
        $bResourceModel->resourceId = $bResource->resourceId;
        $bResourceModel->amount = $bResource->amount;
        $bResourceModel->unitPrice = $bResource->unitPrice;
    //    $bResourceModel->bookingOpenTime = $bResource->resourceId;
    //    $bResourceModel->bookingEndTime = $bResource->resourceId;
        $bResourceModel->repeatStartTime = $bResource->repeatStartTime;
        $bResourceModel->repeatEndTime = $bResource->repeatEndTime;
        
        $timeslotId = $bResource->timeslotId;
        $bResourceModel->timeslotId = $timeslotId;
        $bResourceTimeslotModel->id = $timeslotId;
                    
        if($timeslotId>0)
        {
           $timeslotRow = new BSTimeslot($timeslotId);
           $bResourceTimeslotModel->description = $timeslotRow->description;
        }
        else
        {
            $bResourceTimeslotModel->description = '1-7';
        }
     }

    if ($bResourceTimeslotModel->id == "") {
        $bResourceTimeslotModel->id = 0;
    }
    
    if ($bResourceTimeslotModel->id == 0) {
        $startHr = $bResourceModel->repeatStartTime; //$_POST['repeatStartTime' . $bResourceId];
        $endHr = $bResourceModel->repeatEndTime; //$_POST['repeatEndTime' . $bResourceId];
        $weeklyRepeatDay = $bResourceTimeslotModel->description; //$_POST['weeklyRepeatDay' . $bResourceId];

        $cron = '0 '.$startHr.'-'.$endHr.' * * '.$weeklyRepeatDay;
        $timeslotName = $startHr.'-'.$endHr.$clockStr.' '.$weekDayStr.$weeklyRepeatDay;    
    //}
        $bResourceTimeslotModel->name = $timeslotName;
        $bResourceTimeslotModel->cronValue = $cron;
    }

    if ($screen->isAjaxLinkClicked(array('ajaxLinkId' => 'link' . $bResourceId))) {
        if ($app instanceof BBookingSports) {
            $app->addBookableResource($bResourceModel, $bResourceTimeslotModel);
            Yii::app()->end();
        }
    }
}

if($bResourceId>0)
{
    $defaultPrimaryResourceId = $bResource->resourceId;
//    $defaultPrimaryResourceName = $bResource->name;
    $defaultUnitPrice = $bResource->unitPrice;
//        $resourceModel->resourceType = $bResource->resourceType;
    $defaultAmount = $bResource->amount;
    
    $defaultTimeslotId = $bResource->timeslotId;
    $timeslot = new BSTimeslot($defaultTimeslotId);
    if ($timeslot->cronValue != NULL){
        $pieces = explode(" ", $timeslot->cronValue);
        
        $startEndHours = explode("-", $pieces[1]);
        $defaultStartHour = $startEndHours[0];
        $defaultEndHour = $startEndHours[1];
        
        $defaultRepeatWeekDay = $pieces[4];
    }
    else{
        $defaultStartHour = '8';
        $defaultEndHour = '20';
        $defaultRepeatWeekDay = '1-7';
    }
    
    $bResourceModel->timeslotId = $defaultTimeslotId;
}
else{
    $defaultPrimaryResourceId = 0;
    $defaultPrimaryResourceName = '';
    $defaultUnitPrice = '100';
    $defaultAmount = '1';
    
    $defaultStartHour = '8';
    $defaultEndHour = '20';
    $defaultRepeatWeekDay = '1-7';
}
?>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3><?php echo $manageBookableResourceStr; ?></h3>
</div>
<div class="modal-body">

    <div class="tabbable tabs-right" style="margin-top: 15px;"> <!-- Only required for left/right tabs -->
        <ul class="nav nav-tabs" style="margin-bottom: 15px">
            <li class="active"><a href="#tab1<?php echo $bResourceId; ?>" data-toggle="tab"><?php echo $setupResourceStr; ?></a></li>
            <li><a href="#tab2<?php echo $bResourceId; ?>" data-toggle="tab"><?php echo $setTimeSlotStr; ?></a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane active" id="tab1<?php echo $bResourceId; ?>" style="min-height: 270px">   
                <div class="well">
                    <label><?php echo $choosePrimaryResourceStr; ?>:</label>
                    <?php
                    {
                    $htmlOps = array('name' => 'bResourceId' . $bResourceId);
//                    $htmlOps['value'] = $r['id'];
                    $bResourceModel->id = $defaultPrimaryResourceId;
                    echo CHtml::activeDropDownList($bResourceModel, 'id', $rList, $htmlOps);
                    }
                    ?>             
                    <label><?php echo $setAvailableAmountStr; ?>:</label>                
                    <?php
                    echo CHtml::activeTextField($bResourceModel, 'amount', array('name' => 'amount' . $bResourceId, 'value' => $defaultAmount, 'placeholder' => '0 means unlimitied'));
                    ?>
                    <label><?php echo $setUnitPricetStr; ?>:</label>                
                    <?php
                    echo CHtml::activeTextField($bResourceModel, 'unitPrice', array('value' => $defaultUnitPrice, 'name' => 'unitPrice' . $bResourceId, 'placeholder' => 'Type unit price...', 'required' => TRUE));
                    ?>
                </div>
                <?php 
                if($isPost)
                foreach ($dependentResourceList as $id => $r) { ?>
                    <div class="well">
                        <label><?php echo $manageBookableResourceStr; ?>Choose Dependant Resource:</label>
                        <?php
                        if($isPost){
                        $htmlOps = array('name' => 'drid_' . $bResourceId . '_' . $id);
                        if (isset($r[id]) && $r[id] != NULL)
                            $htmlOps['value'] = $r[id];
                        echo CHtml::activeDropDownList($bResourceModel, 'id', $rList, $htmlOps);
                        }
                        ?>             
                        <label><?php echo $setAvailableAmountStr; ?>:</label>                
                        <?php
                        if($isPost)
                        echo CHtml::activeTextField($bResourceModel, 'amount', array('name' => 'amount' . $bResourceId . '_' . $id, 'value' => 0, 'placeholder' => '0 means unlimitied'));
                        ?>
                        <label><?php echo $setUnitPricetStr; ?>Set unit price:</label>                
                        <?php
                        if($isPost)
                        echo CHtml::activeTextField($bResourceModel, 'unitPrice', array('name' => 'unitPrice' . $bResourceId . '_' . $id, 'placeholder' => '0 means no price', 'value' => 0));
                        ?>
                    </div>
                <?php } ?>
            </div>
            <div class="tab-pane" id="tab2<?php echo $bResourceId; ?>" style="min-height: 270px">
                <div class="well">
                    <label><?php echo Yii::t('site', 'Timeslot'); ?>:</label>                    
                    <?php
                    echo CHtml::activeDropDownList($bResourceModel, 'timeslotId', $timeslotList, array('empty' => Yii::t('site', 'Create new timeslot'), 'name' => 'timeslotId' . $bResourceId));
                    ?>
                    <label><?php echo $setStartHourStr; ?>:(0-23)</label>
                    <?php echo CHtml::activeTextField($bResourceModel, 'repeatStartTime', array('name' => 'repeatStartTime' . $bResourceId, 'value' => $defaultStartHour, 'placeholder' => '(0-23)', 'required' => true)); ?>                
                    <label><?php echo $setEndHourStr; ?>:(0-23)</label>
                    <?php echo CHtml::activeTextField($bResourceModel, 'repeatEndTime', array('name' => 'repeatEndTime' . $bResourceId, 'value' => $defaultEndHour, 'placeholder' => '(0-23)', 'required' => true)); ?>                
                    <!--<label>repeat type:</label>-->
                    <?php // echo CHtml::activeTextField($bResourceModel, 'repeatType', array('name' => 'amount' . $bResourceId, 'value' => 1, 'placeholder' => 'Type amount...', 'required' => true)); ?>                
                    <!--<div><input type="text" name="repeatType" ></div>-->
                    <label><?php echo $weeklyRepeatDaysStr; ?>:(1-7)</label>
                    <?php  echo CHtml::activeTextField($bResourceTimeslotModel, 'weeklyRepeatDay', array('name' => 'weeklyRepeatDay' . $bResourceId, 'value' => $defaultRepeatWeekDay, 'placeholder' => '(1-7)', 'required' => true)); ?>                
                    <!--<div><input type="text" name="weeklyRepeatDay" value=<?php echo $defaultRepeatWeekDay; ?>>(1-7)</div>-->
<!--                    <label>monthly repeat date:</label>
                    <?php // echo CHtml::activeTextField($bResourceModel, 'monthlyRepeatDate', array('name' => 'amount' . $bResourceId, 'value' => 1, 'placeholder' => 'Type amount...', 'required' => true)); ?>                
                    <div><input type="text" name="monthlyRepeatDate" >(1-31)</div>
                    <label>holiday repeat date:</label>
                    <?php // echo CHtml::activeTextField($bResourceModel, 'holidayRepeatDate', array('name' => 'amount' . $bResourceId, 'value' => 1, 'placeholder' => 'Type amount...', 'required' => true)); ?>                
                    <div><input type="text" name="holidayRepeatDate" ></div>-->
                </div>
            </div>
        </div>
    </div>

</div>     
<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true"><?php echo Yii::t('site','Close'); ?></button>
    <?php
    echo $screen->getAjaxLink(Yii::t('site','Save changes'), array(
        'ajaxLinkId' => 'link' . $bResourceId,
        'ajaxContainerId' => 'myModal' . $bResourceId,
//        'ajaxView' => array('manageBookable', 'data' => array('bResourceId' => $bResourceId)),
        'ajaxView' => array('manageBookable', 'data' => array('bResourceId' => $bResourceId)),        
        'htmlOptions' => array('class' => 'btn btn-primary'),
        'ajaxOptions' => array('complete' => 'function(){$("#ajax-loaderdiv").hide();$("#myModal'.$bResourceId.'").modal("hide");$("#refreshIt").click();}')));
    ?>
</div>

<script type="text/javascript">
$('body').on('hidden.bs.modal', '.modal', function () {
    $(this).removeData('bs.modal');
});

if (typeof(timeslots) === "undefined") {
    var timeslots = JSON.parse('<?php echo json_encode($timeslotInfoList); ?>');
    timeslots[""] = {startHour: 8, endHour: 20, repeatWeekDay: '1-7'};
    
    function checkTimeslot(bResourceId) {
        var dropDownList = $('#timeslotId' + bResourceId);
        
        var selected = $(dropDownList).children('option:selected').val();

        var disabled = (selected !== "");

        $('#repeatStartTime' + bResourceId).prop('disabled', disabled);
        $('#repeatEndTime' + bResourceId).prop('disabled', disabled);
        $('#weeklyRepeatDay' + bResourceId).prop('disabled', disabled);
    }
}

$(function() {
    $('#timeslotId<?php echo $bResourceId; ?>').change(function() {
        var selected = $(this).children('option:selected').val();

        checkTimeslot(<?php echo $bResourceId; ?>);

        $('#repeatStartTime<?php echo $bResourceId; ?>').val(timeslots[selected].startHour);
        $('#repeatEndTime<?php echo $bResourceId; ?>').val(timeslots[selected].endHour);
        $('#weeklyRepeatDay<?php echo $bResourceId; ?>').val(timeslots[selected].repeatWeekDay);
    });
    
    checkTimeslot(<?php echo $bResourceId; ?>);
});

</script>



